package defense_against_all;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;


public class World extends JPanel implements ActionListener {

    private Timer timer;
    private Tank craft;
    private Image img;
    static Font font = new Font("SanSerif",Font.PLAIN,15);
    
    Enemy_Ground e1;
    Enemy_Ground e2;
    
    Enemy_Base b1, b2;
    
    boolean lost = false;
    
    public World() {

        addKeyListener(new TAdapter());
        setFocusable(true);
        setDoubleBuffered(true);
        
        ImageIcon ic = new ImageIcon("GameDevelopment/DefenseAgainstAll/src/defense_against_all/image/background.gif");
        img = ic.getImage();
        craft = new Tank();
        
        //enemy - ground
        e1 = new Enemy_Ground(craft.x+550,320,"GameDevelopment/DefenseAgainstAll/src/defense_against_all/image/enemyL.gif");
        e2 = new Enemy_Ground(craft.x+700,320,"GameDevelopment/DefenseAgainstAll/src/defense_against_all/image/enemyL.gif");
        
        //Enemy Base
       // b1 = new Enemy_Base(craft.x+550,320,"C:/Users/Kanwar/GameDevelopment/DefenseAgainstAll/src/defense_against_all/image/ground_base.gif");
        
        timer = new Timer(8, this);
        timer.start();
    }
    public void checkCollisons(){
    	Rectangle r1 = e1.getBounds();
    	Rectangle r2 = e2.getBounds();
    	ArrayList bombs = Tank.getBombs();
        for(int w = 0; w < bombs.size();w++)
        {
        	Fire f = (Fire)bombs.get(w);
        	Rectangle f1  = f.getBounds();
        	if(r1.intersects(f1)&& e1.Alive())
        	{
        		e1.isAlive = false;
        		f.visible = false;
        	}
        	else if(r2.intersects(f1)&& e2.Alive())
        	{
        		e2.isAlive = false;
        		f.visible = false;
        	}
        }
        Rectangle tank = craft.getBounds();
        if(tank.intersects(r1) || tank.intersects(r2)){
        	lost = true;
        }
    }

    public void paint(Graphics g) {
    	if(lost)
    		System.exit(0);
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
       // loop background
        if((craft.getX() - 240)%2400 == 0)
        	craft.backinx = 0;
        if((craft.getX() - 1240)%2400 == 0)
        	craft.backx = 0;
        g2d.drawImage(img, 788-craft.backx, 0,null);
        if(craft.getX() >=240){
        	g2d.drawImage(img, 788-craft.backinx, 0,null);
        }
        g2d.drawImage(craft.getImage(), craft.leftT, 320, null);
        
        //bombs
        ArrayList bombs = Tank.getBombs();
        for(int w = 0; w < bombs.size();w++)
        {
        	Fire f = (Fire)bombs.get(w);
        	g2d.drawImage(f.getImage(), f.getX(), f.getY(), null);
        }
        g2d.setFont(font);
        g2d.setColor(new Color(150,150,180));
        g2d.drawString("Bombs: "+craft.ammunation_bombs, 0, 15);
        
        //enemy
     	
        if(craft.x > 400){
        	if(e1.Alive()== true){
        		g2d.drawImage(e1.getImage(), e1.getX(),e1.getY(),null); //
        	}
        }
        if(craft.x >500){
        	if(e2.Alive()== true){
        		g2d.drawImage(e2.getImage(), e2.getX(),e2.getY(),null);//
        	}
        }
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }


    public void actionPerformed(ActionEvent e) {
    	checkCollisons();
    	ArrayList bombs = Tank.getBombs();
        for(int w = 0; w < bombs.size();w++){
        	Fire f = (Fire)bombs.get(w);
        	if(f.getVisible()==true)
        	f.move();
        	else
        		bombs.remove(w);
        }
    	
    	craft.move();
    	if(craft.x >400){
    		e1.move(craft.getDx(),craft.leftT);
    	}
    	if(craft.x>500){
    		e2.move(craft.getDx(),craft.leftT);
    	}
        repaint();  
    }


    private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            craft.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            craft.keyPressed(e);
        }
    }

}